﻿namespace eg_085_事件
{/*
  * 事件一般用于类或对象向其他类或对象发出通知。
  * 引发事件的类通常称为“发行者”，
  * 而接收事件的类通常称为“订阅者”。
  */
    internal class Program
    {
        class CallEventArgs : EventArgs
        {
            public readonly long Number;    // 来电号码
            public CallEventArgs(long number) { this.Number = number; }
        }

        class Phone
        {
            public event EventHandler<CallEventArgs> CallAlert;
            public readonly long Number;
            public Phone(long number) { this.Number = number; }
            public void Call(long number)   // 来电
            {
                if (CallAlert != null) CallAlert(this, new CallEventArgs(number));  // 触发来电提醒事件
            }


        }
        static void Main(string[] args)
        {
            static void phone_CallAlert(object sender, CallEventArgs e)
            {
                Phone phone = sender as Phone;
                Console.WriteLine("号码{0}收到来自{1}的呼叫...", phone.Number, e.Number);
            }
            Phone phone = new Phone(13888888888);
            phone.CallAlert += new EventHandler<CallEventArgs>(phone_CallAlert);//添加来电提醒事件
            phone.Call(13999999999);
        }
    }
}